USE [ServerMonitor]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[AllServerDataGrowthRate]'))
DROP VIEW [dbo].[AllServerDataGrowthRate]
GO
USE [ServerMonitor]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[AllServerDataGrowthRate]
AS
  SELECT SN.Name,SN.IP,SN.Instance,A.[DBName]
      ,CAST([DataSize] * 8.0 / 1024 AS DECIMAL(10,2)) AS DataSizeMB
      --,CAST([DataUsage]/([DataSize] * 1.0) AS DECIMAL(10,2)) AS DataUsage
      ,[ServerMonitor].[dbo].[DataGrowthRate](B.ServerId,B.DBName,A.DataUsage,A.RecordTime,'DAY') AS DayGrowthRate
      ,[ServerMonitor].[dbo].[DataGrowthRate](B.ServerId,B.DBName,A.DataUsage,A.RecordTime,'WEEK') AS WeekGrowthRate
      ,[ServerMonitor].[dbo].[DataGrowthRate](B.ServerId,B.DBName,A.DataUsage,A.RecordTime,'MONTH') AS MonthGrowthRate
      ,A.[RecordTime] AS StatTime
  FROM [ServerMonitor].[dbo].[AllServerDBUsage] AS A INNER JOIN
  (SELECT MAX(RecordTime) AS RecordTime,[ServerId],[DBName]
  FROM [ServerMonitor].[dbo].[AllServerDBUsage]
  GROUP BY [ServerId],[DBName]) AS B
  ON A.[RecordTime] = B.RecordTime AND A.[DBName] = B.DBName AND A.ServerId = B.ServerId
  LEFT JOIN [ServerMonitor].[dbo].[Server] AS SN
  ON A.ServerId = SN.Id







GO


